package cz.matasek.dbLogger;

/**
 * Container for various useful utilities.
 *
 * @author Luboš Matásek
 */
public class Util {

    /**
     * Returns the list of values separated by the specified character.
     *
     * @param delimiter items delimiter
     * @param items array of items
     * @return char separated string
     */
    public static String implode(String delimiter, Object[] items) {
        StringBuilder commaSeparatedList = new StringBuilder();

        int itemsCount = 0;
        for (Object item : items) {
            itemsCount++;

            commaSeparatedList.append(item);

            // if not last item, append separator
            if (itemsCount != items.length) {
                commaSeparatedList.append(delimiter);
            }
        }

        return commaSeparatedList.toString();
    }

    /**
     * Quotes all given items.
     *
     * @param items array of items
     * @return array of quoted items
     */
    public static String[] quote(Object[] items) {
        String[] quotedItems = new String[items.length];

        for (int i = 0; i < items.length; ++i) {
            quotedItems[i] = Constant.QUOTE + items[i] + Constant.QUOTE;
        }

        return quotedItems;
    }

    /**
     * Tests if the specified string starts with the specified prefix, ignoring
     * case differences.
     *
     * @param word string to be checked
     * @param prefix the prefix
     * @return <code>true</code> if the specified string starts with the
     * specified prefix (ignore case); <code>false</code> otherwise
     */
    public static boolean startsWithIgnoreCase(String word, String prefix) {
        return word.toLowerCase().startsWith(prefix.toLowerCase());
    }

    /**
     * Tests if the specified text contains the specified pattern, ignoring
     * case differences.
     *
     * @param text string to be checked
     * @param pattern the pattern
     * @return <code>true</code> if the specified string contains the
     * specified pattern (ignore case); <code>false</code> otherwise
     */
    public static boolean containsIgnoreCase(String text, String pattern) {
        return text.toLowerCase().contains(pattern.toLowerCase());
    }
}
